<?php
/**
 * Created by PhpStorm .
 * User: 杜磊磊
 * Date: 2020/7/12
 * Time: 15:29
 */
namespace app\common\business;
use app\common\model\mysql\Category as CategoryModel;
class Category
{
    public $model = null;

    public function __construct()
    {
        $this->model = new CategoryModel;
    }

    public function add($data)
    {
        $data['status'] = config("status.table_normal");
        $name = $data['name'];
        //根据$name 去数据库查询是否存在这条记录
        if($this->model->getSortByName($name)){
            throw new \think\Exception("该分类名称已存在");
        }
        try {
            $this->model->save($data);
        }catch (\Exception $e){
            throw new \think\Exception("服务器内部异常");
        }
        return $this->model->id();
    }

    public function getNormalCategory()
    {
        $field = "id,name,pid";
        $categorys = $this->model->getNormalCategory($field);
        if(!$categorys){
            $categorys = [];
        }
        $categorys = $categorys->toArray();
        return $categorys;
    }

    /**
     * @param $data *pid
     * @param $num  *每页显示数量
     * @return array
     * Author: 杜磊磊
     * Date: 2020/9/17 15:32
     */
    public function getLists($data,$num)
    {
        $list = $this->model->getLists($data,$num);
        if (!$list){
            return [];
        }
//        halt($list);
        $result = $list->toArray();
        $result['render'] = $list->render();
        return $result;
    }

    /**
     * 排序
     * @param $id *数据id
     * @param $listorder *排序数值
     * Author: 杜磊磊
     * Date: 2020/9/24 9:27
     */
    public function listorder($id,$listorder)
    {
        $res = $this->getById($id);
        if(!$res){
            throw new \think\Exception('不存在该条记录');
        }
        $data = [
          "listorder" => $listorder,
        ];
        try {
            $res = $this->model->updateById($id,$data);
        }catch (\Exception $e){
            throw new \think\Exception('服务器内部异常');
        }
        return $res;
    }

    /**
     * 根据id获取某一条记录
     * @param $id
     * @return array
     * Author: 杜磊磊
     * Date: 2020/9/24 9:27
     */
    public function getById($id)
    {
        $result = $this->model->find($id);
        if (empty($result)){
            return [];
        }
        $result = $result->toArray();
        return $result;
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return bool
     * @throws \think\Exception
     * Author: 杜磊磊
     * Date: 2020/10/14 11:46
     */
    public function status($id,$status)
    {
        $res = $this->getById($id);
        if (!$res){
            throw new \think\Exception('不存在该条记录');
        }
        if($res['status'] == $status){
            throw new \think\Exception('状态修改前和修改后一样妹有任何意义哦');
        }
        $data = [
            "status" => intval($status),
        ];
        try{
            $res = $this->model->updateById($id,$data);
        }catch (\Exception $e){
            return false;
        }
        return $res;
    }

}